plotly Package (2)

Data Visualization

Various Interactive Plots using Plotly Package (2)

Yeongeun Jeon
08-21-2022

1. Package plotly

Package plotly(Ver. 4.9.3)는 Interactive Graph를 생성할 수 있는 Package이다. 여기서 Interactive Graph란 마우스 움직임에 반응하며 실시간으로 형태가 변하는 그래프를 말한다. Interactive Graph를 생성하면 그래프를 자유롭게 조작하면서 관심 있는 부분을 자세히 살펴볼 수 있으며, HTML 포맷으로 저장하면 일반 사용자들도 웹 브라우저를 이용해 그래프를 조작할 수 있다.

pacman::p_load("plotly")

2. 주식 데이터

pacman::p_load("quantmod")                  # 주식 데이터 불러오는 Package
# 삼성samsung <- getSymbols("005930.KS",          # 가져오고자 하는 종목 코드 번호, KOSPI는 뒤에 KS, KOSDAQ은 KQ                      from = "2018-01-01",  # 주식 정보를 가져오고자 하는 시작 날짜
                      to = "2022-11-16",    # 주식 정보를 가져오고자 하는 마지막 날짜날짜
                      auto.assign = FALSE)  # 코드 번호("005930.KS") 객체에 데이터 저장할 것인지 여부NAcolnames(samsung) <- c("Open",              # 시가"High",              # 고가"Low",               # 저가
                       "Close",             # 종가"Volume",            # 거래량래량
                       "Adjusted")          # 수정율

head(samsung)
            Open  High   Low Close   Volume Adjusted
2018-01-02 51380 51400 50780 51020  8474250 44269.27
2018-01-03 52540 52560 51420 51620 10013500 44789.88
2018-01-04 52120 52180 50640 51080 11695450 44321.33
2018-01-05 51300 52120 51200 52120  9481150 45223.71
2018-01-08 52400 52520 51500 52020  8383650 45136.95
2018-01-09 51460 51720 49980 50400 18013600 43731.30
# 카카오
kakao <- getSymbols("035720.KS",                 
                    from = "2018-01-01",  
                    to = "2022-11-16",    
                    auto.assign = FALSE)  

colnames(kakao) <- c("Open",              
                     "High",             
                     "Low",              
                     "Close",          
                     "Volume",           
                     "Adjusted")          

head(kakao)
               Open     High      Low    Close   Volume Adjusted
2018-01-02 28205.90 29616.20 28105.17 29515.46  6680627 29427.17
2018-01-03 30321.35 30422.08 29313.99 30019.14  5729670 29929.34
2018-01-04 30724.29 32033.85 30119.88 31429.44 11242421 31335.41
2018-01-05 31630.91 31832.38 30623.55 31429.44  5297057 31335.41
2018-01-08 32336.05 32738.99 31832.38 32134.58  7829439 32038.45
2018-01-09 31832.38 32235.32 31127.23 31630.91  5017557 31536.28
# LG
lg <- getSymbols("003550.KS",                 
                 from = "2018-01-01",  
                 to = "2022-11-16",    
                 auto.assign = FALSE)  

colnames(lg) <- c("Open",              
                  "High",             
                  "Low",              
                  "Close",          
                  "Volume",           
                  "Adjusted")          

head(lg)
               Open      High      Low     Close Volume Adjusted
2018-01-02 99716.21 100374.40 97851.33  99606.51 160040 88163.59
2018-01-03 98728.92  99825.91 98290.12  98728.92 217377 87386.82
2018-01-04 98948.32  99606.51 97083.44  97741.63 238149 86512.95
2018-01-05 97851.33  97851.33 96315.55  97412.53 237488 86221.65
2018-01-08 97851.33  99277.41 97412.53  98290.12 211137 86998.44
2018-01-09 98509.52 103994.46 98290.12 102678.08 382637 90882.29

2-1. 전처리

# Data Frame 변환samsung <- samsung %>%
  data.frame() %>%                          # Data Frame 변환mutate(Date = rownames(.)) %>%            # Date 변수 생성생성
  select(Date, everything())                # Date 변수 1열로 이동이동

head(samsung)
                 Date  Open  High   Low Close   Volume Adjusted
2018-01-02 2018-01-02 51380 51400 50780 51020  8474250 44269.27
2018-01-03 2018-01-03 52540 52560 51420 51620 10013500 44789.88
2018-01-04 2018-01-04 52120 52180 50640 51080 11695450 44321.33
2018-01-05 2018-01-05 51300 52120 51200 52120  9481150 45223.71
2018-01-08 2018-01-08 52400 52520 51500 52020  8383650 45136.95
2018-01-09 2018-01-09 51460 51720 49980 50400 18013600 43731.30
kakao <- kakao %>%
  data.frame() %>%                         
  mutate(Date = rownames(.)) %>%            
  select(Date, everything())                

lg <- lg %>%
  data.frame() %>%                          
  mutate(Date = rownames(.)) %>%            
  select(Date, everything())                


# 데이터 합치기NAdata <- rbind( cbind(samsung, Name = "samsung"),
               cbind(kakao, Name = "kakao"),
               cbind(lg, Name = "lg"))

head(data) 
                 Date  Open  High   Low Close   Volume Adjusted
2018-01-02 2018-01-02 51380 51400 50780 51020  8474250 44269.27
2018-01-03 2018-01-03 52540 52560 51420 51620 10013500 44789.88
2018-01-04 2018-01-04 52120 52180 50640 51080 11695450 44321.33
2018-01-05 2018-01-05 51300 52120 51200 52120  9481150 45223.71
2018-01-08 2018-01-08 52400 52520 51500 52020  8383650 45136.95
2018-01-09 2018-01-09 51460 51720 49980 50400 18013600 43731.30
              Name
2018-01-02 samsung
2018-01-03 samsung
2018-01-04 samsung
2018-01-05 samsung
2018-01-08 samsung
2018-01-09 samsung

3. 시각화

3-1. Line Plot

plot_ly(samsung, type = 'scatter', mode = 'lines') %>%
  add_trace(x = ~Date,                      # X축            y = ~Close,                     # Y축            name = 'Samsung',               # 이름
            line = list(color = 'rgb(22, 96, 167)') ) %>%  # 선 색깔     
  layout(showlegend = FALSE)

plot_ly(samsung, type = 'scatter', mode = 'lines') %>%
  add_trace(x = ~Date,                      # X축            y = ~Close,                     # Y축            name = 'Samsung',               # 이름
            line = list(color = 'rgb(22, 96, 167)') ) %>%  # 선 색깔             
  layout(showlegend = FALSE,
         xaxis = list(rangeslider = list(type = "date")))  # Add Slider

# Line Plot with Three Stock Data
## Data 변환data1 <- data %>%
  select(Date, Name, Close)

head(data1)
                 Date    Name Close
2018-01-02 2018-01-02 samsung 51020
2018-01-03 2018-01-03 samsung 51620
2018-01-04 2018-01-04 samsung 51080
2018-01-05 2018-01-05 samsung 52120
2018-01-08 2018-01-08 samsung 52020
2018-01-09 2018-01-09 samsung 50400
data1 <- data1 %>%
  tidyr::pivot_wider(names_from = "Name",
                     values_from = "Close") %>%
  data.frame()

head(data1)
        Date samsung    kakao        lg
1 2018-01-02   51020 29515.46  99606.51
2 2018-01-03   51620 30019.14  98728.92
3 2018-01-04   51080 31429.44  97741.63
4 2018-01-05   52120 31429.44  97412.53
5 2018-01-08   52020 32134.58  98290.12
6 2018-01-09   50400 31630.91 102678.08
plot_ly(data1, type = 'scatter', mode = 'lines') %>%
  add_trace(x = ~Date,                      # X축            y = ~samsung,                   # Y축            name = 'Samsung',               # 이름
            line = list(color = 'rgb(22, 96, 167)') )%>%  # 선 색깔   
  add_trace(x = ~Date,                      
            y = ~kakao,                     
            name = 'Kakao',                
            line = list(color = 'rbg(22, 167, 82)') ) %>%
  add_trace(x = ~Date,                     
            y = ~lg,                        
            name = 'LG',                    
            line = list(color = 'rgb(153, 22, 167)') ) %>%
  layout(yaxis = list(title = "Close"))                  # Y축 이름    

plot_ly(data1, type = 'scatter', mode = 'lines') %>%
  add_trace(x = ~Date,                      # X축            y = ~samsung,                   # Y축            name = 'Samsung',               # 이름
            line = list(color = 'rgb(22, 96, 167)') ) %>%  # 선 색깔   
  add_trace(x = ~Date,                      
            y = ~kakao,                     
            name = 'Kakao',                
            line = list(color = 'rbg(22, 167, 82') ) %>%
  add_trace(x = ~Date,                     
            y = ~lg,                        
            name = 'LG',                    
            line = list(color = 'rgb(153, 22, 167)') ) %>%
  layout(yaxis = list(title = "Close"),                   # Y축 이름          xaxis = list(rangeslider = list(type = "date"))) # Add Slider 

3-2. TimeZone

plot_ly(samsung, type = 'scatter', mode = 'lines', fill = 'tozeroy',
        fillcolor='rgba(114, 186, 59, 0.5)',              
        line = list(color = 'rgb(114, 186, 59)')) %>%      # 선 색깔add_trace(data = samsung, 
            x = ~Date, 
            y = ~Close, 
            name = 'Samsung') %>%
  layout(showlegend = FALSE)

3-3. Candlestick

plot_ly(samsung, type = 'candlestick') %>%
  add_trace(x = ~Date,
        open = ~Open, close = ~Close, 
        high = ~High, low = ~Low) %>%
  layout(showlegend = FALSE)

plot_ly(samsung, type = 'candlestick') %>%
  add_trace(x = ~Date,
            open = ~Open, close = ~Close, 
            high = ~High, low = ~Low) %>%
  layout(showlegend = FALSE,
         xaxis = list(rangeslider = list(visible = FALSE)))   # No Slider 

plot_ly(samsung, type = 'candlestick') %>%
  add_trace(x = ~Date,
            open = ~Open, close = ~Close, 
            high = ~High, low = ~Low,
            increasing = list(line = list(color = 'red')),      # 증가할 때 색깔
            decreasing = list(line = list(color = 'blue'))) %>% # 감소할 때 색깔layout(showlegend = FALSE)

plot_ly(samsung, type = 'candlestick') %>%
  add_trace(x = ~Date,
            open = ~Open, close = ~Close, 
            high = ~High, low = ~Low,
            increasing = list(line = list(color = 'red')),      # 증가할 때 색깔
            decreasing = list(line = list(color = 'blue'))) %>% # 감소할 때 색깔add_lines(x = ~Date, y = ~Close,                              # 선 추가가
            line = list(color = 'black', width = 0.75)) %>%
  layout(showlegend = FALSE)

3-4. Bar Plot

plot_ly(samsung, type = "bar") %>%
  add_trace(x = ~Date,
            y = ~Volume,
            name = "Samsung",
            marker = list(color = 'rgb(49,130,189)'))  %>% # Bar 색깔
  layout(showlegend = FALSE)

plot_ly(samsung, type = "bar") %>%
  add_trace(x = ~Date,
            y = ~Volume,
            name = "Samsung",
            marker = list(color = 'rgb(49,130,189)'))  %>% # Bar 색깔
  layout(showlegend = FALSE,
         xaxis = list(rangeslider = list(type = "date"))) # Add Slider

# Bar Plot with Three Stock Data
##  Data 변환data2 <- data %>%
  select(Date, Name, Volume)

head(data2)
                 Date    Name   Volume
2018-01-02 2018-01-02 samsung  8474250
2018-01-03 2018-01-03 samsung 10013500
2018-01-04 2018-01-04 samsung 11695450
2018-01-05 2018-01-05 samsung  9481150
2018-01-08 2018-01-08 samsung  8383650
2018-01-09 2018-01-09 samsung 18013600
data2 <- data2 %>%
  tidyr::pivot_wider(names_from = "Name",
                     values_from = "Volume") %>%
  data.frame()

head(data2)
        Date  samsung    kakao     lg
1 2018-01-02  8474250  6680627 160040
2 2018-01-03 10013500  5729670 217377
3 2018-01-04 11695450 11242421 238149
4 2018-01-05  9481150  5297057 237488
5 2018-01-08  8383650  7829439 211137
6 2018-01-09 18013600  5017557 382637
plot_ly(data2, type = "bar") %>%
  add_trace(x = ~Date,
            y = ~samsung,
            name = "Samsung",
            marker = list(color = 'rgb(22, 96, 167)'))  %>%
  add_trace(x = ~Date,
            y = ~kakao,
            name = "Kakao",
            marker = list(color = 'rbg(22, 167, 82)'))  %>%
  add_trace(x = ~Date,
            y = ~lg,
            name = "LG",
            marker = list(color = 'rgb(153, 22, 167)'))  %>%
  layout(xaxis = list(rangeslider = list(type = "date")),
         yaxis = list(title = "Volume"))              # Y축 이름

3-5. Pie Chart

# Data 변환data3 <- data %>%
  filter(Date == "2022-11-16") %>%
  select(Name, Volume) 

head(data3)
               Name   Volume
2022-11-16  samsung 12909260
2022-11-161   kakao  2451864
2022-11-162      lg   237822
plot_ly(data3, labels = ~Name, values = ~Volume, type = 'pie') %>%
  layout(showlegend = FALSE,
         title = "Volume at 2022-11-16")              # Title

plot_ly(data3, labels = ~Name, values = ~Volume, type = 'pie',
        textposition = 'inside',                      # 글자 위치        textinfo = 'label+percent',                   # 글자 타입NA= list(color = '#FFFFFF'),     # 글자 색깔        marker = list(colors = c('rgb(211,94,96)', 'rgb(128,133,133)', 'rgb(144,103,167)'))  # 파이 색깔NA) %>%
  layout(showlegend = FALSE,
         title = "Volume at 2022-11-16")